<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar shadow">
        <div class="btn-group tool-button mt5">
            <el-input placeholder="请输入用户名或手机号" v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch">
                <el-button slot="append" icon="el-icon-search" @click="doSearch"></el-button>
            </el-input>
        </div>
        <div class="btn-group tool-button mt5">
            <el-button slot="append" @click="restrictionEdit" :disabled="selectData.length==0" title="未单独设置或单独设置项被禁用后的用户以默认项为标准">
                默认限制
            </el-button>
        </div >
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                header-align="center"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                :default-sort="{prop: 'opAt', order: 'descending'}"
        >
            <el-table-column
                    type="selection"
                    width="35">
            </el-table-column>

            <el-table-column label="用户名称" header-align="left" prop="nickName"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="手机号码" header-align="left" prop="phone"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="单次最低限制" header-align="left" prop="timeMinRestriction"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="单次最高限制" header-align="left" prop="timeMaxRestriction"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="每日限制" header-align="left" prop="dayRestriction"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="总限制" header-align="left" prop="totalRestriction"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="个人返水" header-align="left" prop="selfRate"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.selfRate/100}}%
                </template>
            </el-table-column>

            <el-table-column label="上级返水" header-align="left" prop="superRate"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.superRate/100}}%
                </template>
            </el-table-column>

            <el-table-column label="观点分成" header-align="left" prop="viewRate"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.viewRate}}%
                </template>
            </el-table-column>

            <el-table-column label="礼物分成" header-align="left" prop="viewRate"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.giftRate}}%
                </template>
            </el-table-column>

            <el-table-column sortable label="最后变更时间" header-align="center" align="center" prop="opAt">
                <template slot-scope="scope">
                    <span v-if="scope.row.opAt"> {{formatAt(scope.row.opAt)}}</span>
                    <span v-else>未设置</span>
                </template>
            </el-table-column>  <!--User启用状态-->
            <el-table-column label="操作" header-align="center" prop="id"
                             :show-overflow-tooltip="true" align="center" width="120">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item :command="{userId:scope.row.uId}">
                                修改
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>

        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>


    <el-dialog
            title="限制"
            :visible.sync="showRestriction"
            width="40%">
        <el-form :model="formData1" ref="restrictionForm" size="small" label-width="80px">

            <el-form-item prop="code" label="单次最低">
                <el-input maxlength="100" placeholder="单次最低"
                          v-model="formData1.timeMinRestriction"
                          auto-complete="off" tabindex="0" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="单次最高">
                <el-input maxlength="100" placeholder="单次最高"
                          v-model="formData1.timeMaxRestriction"
                          auto-complete="off" tabindex="0" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="每日">
                <el-input maxlength="100" placeholder="每日"
                          v-model="formData1.dayRestriction"
                          auto-complete="off" tabindex="1" type="text"></el-input>
            </el-form-item>

            <el-form-item prop="code" label="总额">
                <el-input maxlength="100" placeholder="总额"
                          v-model="formData1.totalRestriction"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>

            <el-form-item prop="code" label="个人返水">
                <el-input maxlength="2" placeholder="单位：万"
                          type="number"
                          v-model="formData1.selfRate"
                          auto-complete="off" tabindex="3" type="text"></el-input>
            </el-form-item>

            <el-form-item prop="code" label="上级返水">
                <el-input maxlength="2" placeholder="单位：万"
                          type="number"
                          v-model="formData1.superRate"
                          auto-complete="off" tabindex="4" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="礼物分成">
                <el-input maxlength="3" placeholder="百分之"
                          type="number"
                          v-model="formData1.giftRate"
                          auto-complete="off" tabindex="5" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="观点分成">
                <el-input maxlength="3" placeholder="百分之"
                          type="number"
                          v-model="formData1.viewRate"
                          auto-complete="off" tabindex="5" type="text"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                    <el-button @click="showRestriction = false">取 消</el-button>
                    <el-button type="primary" @click="editRestrictionDo">确 定</el-button>
                  </span>
    </el-dialog>

</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                addDialogVisible: false,
                editDialogVisible: false,
                selectData: [],
                pageForm: {
                    searchName: "",
                    searchKeyword: "",
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "opAt",
                    pageOrderBy: "descending"
                },
                tableData: [],
                formData: {
                    id: "",
                },
                formData1: {
                    id: "",
                },
                formRules: {
                    toUser: [
                        {required: true, message: '充值对象', trigger: 'blur'}
                    ],
                    amount: [
                        {required: true, message: '金额', trigger: 'blur'}
                    ]
                },
                options:[],
                loading:false,
                showRestriction:false
            }
        },
        methods: {
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                self.pageForm.key=self.pageForm.searchKeyword
                $.post(base + "/platform/user/restriction/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            channelFormat: function (val) {
                switch (val) {
                    case "MANUAL":{
                        return "手动"
                    }
                    case "ALI_PAY":{
                        return "支付宝"
                    }
                    case "WE_CHAT_PAY":{
                        return "微信"
                    }
                    case "UNION_PAY":{
                        return "银联"
                    }

                }
            },
            handleSelectionChange: function (val) {
                this.selectData = val;
            },
            openAdd: function () {
                var self = this;
                self.addDialogVisible = true;
                self.formData = {
                    id: ""
                };//打开新增窗口,表单先清空
                if (self.$refs["addForm"])
                    self.$refs["addForm"].resetFields();
            },
            doAdd: function () {
                var self = this;
                var url = base + "/platform/user/restriction/add";
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            doEdit: function () {
                var self = this;
                var url = base + "/platform/cms/link/class/editDo";
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.editDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var userId = command.userId;
                if(userId != null){
                    var _this = this;
                    let param = {};
                    if(typeof userId == "string"){
                        param = {userId};
                    }
                    this.formData1 = {};
                    this.showRestriction = true;
                    $.post(base + "/platform/user/restriction/showRestriction" , param, function (data) {
                        _this.formData1 = data.data;
                    }, "json");
                } else{
                    var self = this;
                    // 启用，禁用按钮
                    if ("enable" == command.type || "disable" == command.type) {
                        $.post(base + "/platform/user/restriction/disable",command, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                if ("disable" == command.type) {
                                    command.row.disabled = true;
                                }
                                if ("enable" == command.type) {
                                    command.row.disabled = false;
                                }
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }


                }
            },
            getAccounts:function (str) {
                var _this = this;
                $.post(base + "/platform/user/manager/searchAccount" , {key:str}, function (data) {
                    _this.options = data.code == 0 ? data.data.list.map(ite => {
                            // ite.label =ite.nickName+'#<span style="color: darkblue">'+ite.phone+'</span>$<span style="color: darkred">'+ite.balance+'</span>'
                            ite.label =ite.nickName+'#'+ite.phone+'$'+ite.balance
                            return ite
                        }
                    ) : [];
                }, "json");
            },
            restrictionEdit:function () {
                var self = this;
                var ids = [];
                self.selectData.forEach(function (val) {
                    ids.push(val.id);
                });
                self.$confirm('您确定要选中的 ' + ids.length + ' 条记录恢复默认限制？ ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    callback: function (a, b) {
                        if ("confirm" == a) {//确认后再执行
                            $.post(base + "/platform/user/restriction/batchRestriction", {ids: ids.toString()}, function (data) {
                                if (data.code == 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.pageData();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
            editRestrictionDo: function () {
                var self = this;
                this.showRestriction = false;
                $.post(base + "/platform/user/restriction/editDo" , this.formData1, function (data) {
                    if (data.code == 0) {
                        self.$message({
                            message: data.msg,
                            type: 'success'
                        });
                        self.addDialogVisible = false;
                        self.pageData();
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            }
        },
        created: function () {
            this.pageData();
        }
    });
</script>
<!--#
}
#-->